/*
 * Copyright 2012 JooKar BV (info@jookar.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.jookar.android.simpleclock.views;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;

/**
 * Simple base implementation of the swapper. <br/>
 * Subclasses only need to load the appropriate animations.
 * @author Joost A. Bloemsma
 *
 */
public abstract class SimpleBaseAnimatedSwapper implements IAnimatedViewSwapper {
	private static final String DEBUG_TAG = SimpleBaseAnimatedSwapper.class.getName();
	private Animation animShow;
	private Animation animHide;
	
	/**
	 * Constructs a new SimpleBaseAnimatedSwapper instance
	 */
	public SimpleBaseAnimatedSwapper() {
		super();
		Log.d(DEBUG_TAG, "Constructed " + this.getClass().getSimpleName());
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void init(View visibleView, View hiddenView) {
		loadAnimations();
		visibleView.setVisibility(View.VISIBLE);
		hiddenView.setVisibility(View.INVISIBLE);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void init(Context ctx, View visibleView, View hiddenView) {
		this.init(visibleView, hiddenView);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void swapViews(View toHide, View toShow) {
		toShow.startAnimation(animShow);
		toShow.setVisibility(View.VISIBLE);
		toHide.startAnimation(animHide);
		toHide.setVisibility(View.INVISIBLE);
	}
	
	/**
	 * To be implemented by subclasses. This is where the animations are created, and the passed to 
	 * either {@link #setShowAnimation(Animation)} or {@link #setHideAnimation(Animation)}.
	 */
	protected abstract void loadAnimations();
	
	/**
	 * To be called by subclasses from the implementation of {@link #loadAnimations()}.
	 * @param anim The {@link Animation} to use for showing the view
	 */
	protected void setShowAnimation(Animation anim) {
		Log.d(DEBUG_TAG, "Show animation registered");
		animShow = anim;
	}
	
	/**
	 * To be called by subclasses from the implementation of {@link #loadAnimations()}.
	 * @param anim The {@link Animation} to use for hiding the view
	 */
	protected void setHideAnimation(Animation anim) {
		Log.d(DEBUG_TAG, "Hide animation registered");
		animHide = anim;
	}
}
